package com.redhipps.hips.client.model;

import java.util.Date;

/**
 * A basic, though non-retarded version of {@link java.util.Date}.
 * Fields are based so that they can be used intuitively to go to/from human readable format.
 * Year, month, day are 1-based, Hour, minute, second are 0-based.
 * 
 * @author yuhong
 */
public class DateTime {

  private int year;
  private int month;
  private int day;
  private int hour;
  private int minute;
  private int second;

  public DateTime(int year, int month, int day) {
    this(year, month, day, 0, 0, 0);
  }

  public DateTime(int year, int month, int day, int hour, int minute, int second) {
    this.year = year;
    this.month = month;
    this.day = day;
    this.hour = hour;
    this.minute = minute;
    this.second = second;
  }

  public int getYear() {
    return year;
  }

  public int getMonth() {
    return month;
  }

  public int getDay() {
    return day;
  }

  public int getHour() {
    return hour;
  }

  public int getMinute() {
    return minute;
  }

  public int getSecond() {
    return second;
  }

  @SuppressWarnings("deprecation")
  public int getDayOfWeek() {
    return new Date(year - 1900, month - 1, day).getDay();
  }

  @Override
  public boolean equals(Object o) {
    if (!(o instanceof DateTime)) {
      return false;
    }
    DateTime that = (DateTime) o;
    return year == that.year && month == that.month && day == that.day &&
        hour == that.hour && minute == that.minute && second == that.second;
  }

  @Override
  public int hashCode() {
    int code = year;
    code <<= 12;
    code += month;
    code <<= 4;
    code += day;
    code <<= 5;
    code += hour;
    code <<= 5;
    code += minute;
    code <<= 6;
    code += second;
    return code;
  }

  @SuppressWarnings("deprecation")
  public static DateTime now() {
    Date date = new Date();
    return new DateTime(date.getYear(), date.getMonth(), date.getDate(),
        date.getHours(), date.getMinutes(), date.getSeconds());
  }

  public static int daysInMonth(int year, int month) {
    switch (month) {
      case 2:
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
          return 29; // leap year
        else
          return 28;
      case 4:
      case 6:
      case 9:
      case 11:
        return 30;
      default:
        return 31;
    }
  }

}
